<?php


namespace app\api\controller\v1;



use app\api\model\BikeMgr;
use app\api\model\BikeModel;
use app\api\model\UserRideViolationOrderModel;
use app\common\controller\UserBaseController;
use app\common\library\DLock;
use app\common\library\IDMan;

class UrviolationController extends UserBaseController
{

    function submitAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'bike_id' => 'string',
            'violation_type' => 'int',
            'violation_desc' => ['optstring', ""],
            'violation_remark' => 'string',
            'imgs' => 'stringarray',
        );

        $this->check_params();
        $this->check_token();

        $l = new DLock("violation_submt_" . $this->argv['bike_id'], 30);

        $info = (new BikeMgr())->user_ride_violation_report($this->city_id, $this->argv['bike_id'], $this->argv['violation_type'], $this->argv['violation_desc'], $this->argv['violation_remark'], $this->argv['imgs']);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function punishAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'fine' => 'int',
            'blacklisted' => 'int',
        );

        $this->check_params();
        $this->check_token();


        (new UserRideViolationOrderModel())->punish($this->city_id, $this->argv['order_id'], $this->argv['fine'], $this->argv['blacklisted']);

        return $this->ret_resp();
    }

    function auditAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'agreed' => 'int',
        );

        $this->check_params();
        $this->check_token();

        (new UserRideViolationOrderModel())->audit($this->city_id, $this->argv['order_id'], $this->argv['agreed']);

        return $this->ret_resp();
    }

    function revokeAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'city_id' => 'optstring',
        );

        $this->check_params();
        $this->check_token();

        (new UserRideViolationOrderModel())->revoke($this->city_id, $this->argv['order_id']);

        return $this->ret_resp();
    }

    function listAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'violation_type' => 'optint',
            'order_state' => 'optint',
            'punish_type' => 'optint',
            'sk' => 'optstring',
            'user_id' => 'optstring',
            'pno' => 'int',
            'psize' => 'int',
        );

        $this->check_params();
        $this->check_token();

        $list = (new UserRideViolationOrderModel())->query_list($this->city_id, $this->argv, $this->argv['pos'], $this->argv['limit']);
        $this->resp['list'] = $list;
        $this->resp['total'] = hisql::query_count();

        return $this->ret_resp();
    }

    function list_by_skAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'sk' => 'string',
            'pno' => 'int',
            'psize' => 'int',
        );

        $this->check_params();
        $this->check_token();


        $list = [];
        $total = 0;
        $city_id = null;
        if(IDMan::is_bike_sn($this->argv['sk'])){
            $bike_info = (new BikeModel())->get_info_by_sn($this->argv['sk']);
            if($bike_info){
                $city_id = $bike_info['city_id'];
            }
        }
        if (!empty($this->argv['sk'])) {
            $list = (new UserRideViolationOrderModel())->query_list($city_id, $this->argv, $this->argv['pos'], $this->argv['limit']);
            $total = hisql::query_count();
        }
        $this->resp['list'] = $list;
        $this->resp['total'] = $total;

        return $this->ret_resp();
    }

    function myviolationsAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'pno' => 'int',
            'psize' => 'int',
        );

        $this->check_params();
        $this->check_token();

        $cd = [
            'user_id' => \ApiOper::instance()->userinfo()['id']
        ];
        $list = (new UserRideViolationOrderModel())->query_list(NULL, $cd, $this->argv['pos'], $this->argv['limit']);
        $this->resp['list'] = $list;
        $this->resp['total'] = hisql::query_count();

        return $this->ret_resp();
    }

}
